package com.servlet;

import com.dao.imp.AddUpdDelDaoImp;
import net.sf.json.JSONObject;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

/**
 * @author Mathison
 * @Title: ${NAME}
 * @Package: ${PACKAGE_NAME}
 * @Description: TODO
 * @date 2021/11/21 22:00
 **/

@WebServlet(name = "AddGoodServlet", value = "/AddGoodServlet")
public class AddGoodServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("GET");
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
            String name = request.getParameter("name");
            String subtitle = request.getParameter("subtitle");
            int price = Integer.parseInt( request.getParameter("price"));
            int stock = Integer.parseInt(request.getParameter("stock"));
            String childTypeId = request.getParameter("childTypeId");
            String parentTypeId = request.getParameter("parentTypeId");
            String detail = request.getParameter("detail");
            String imgUrls = request.getParameter("subImages");
            String jsonString;

            HttpSession session = request.getSession();
//            System.out.println("userId:"+session.getAttribute("userId"));

            String sql = "INSERT INTO goods ( merchant_id, good_name, good_information, good_price, good_sales, good_remainingAmont, good_parentType, good_childType, subtitle, imgUrls) " +
                    "VALUES (" +
                    session.getAttribute("MerchantId")+",\'" +
                    name+" \', \'" +
                    detail+"\', " +
                    price+", " +
                    0+"," +
                    stock+",\'" +
                    parentTypeId+"\' ,\'" +
                    childTypeId+ "\',\'" +
                    subtitle+"\',\'" +
                    imgUrls+
                    "\')";

            AddUpdDelDaoImp ad = new AddUpdDelDaoImp();
            boolean result = ad.addUpdDelGoods(sql);

            if (result==true){
                jsonString = "{status:0,msg:'add success'}";
            }else {
                jsonString = "{status:1,msg:'add fail'}";
            }

            JSONObject json = JSONObject.fromObject(jsonString);
            response.getWriter().print(json);
    }
}
